<template>
	<div class="aid">
		<div class="base">
			<p class="fixed-top">
				{{ text.ID ? '修改寒暑假期学期' : '新增寒暑假期学期' }}
				<a-icon @click="closeFunction" type="close" />
			</p>
			<div class="main" id="new_message">
				<a-form :form="form">
					<a-form-item label="名称"><a-input v-decorator="['Name', { rules: [{ required: true, message: '请输入名称' }] }]" /></a-form-item>
					<a-form-item label="类型">
						<a-select :allowClear="true" v-decorator="['Type', { rules: [{ required: true, message: '请选择寒暑假类型' }] }]" placeholder="请选择寒暑假类型" optionFilterProp="children" showSearch>
							<a-select-option v-for="(item, index) in TypeList" :key="index" :value="item.Key">{{ item.Value }}</a-select-option>
						</a-select>
					</a-form-item>
					<a-form-item label="学分"><a-input v-decorator="['Score', { rules: [{ required: true, message: '请输入学分' }] }]" /></a-form-item>
					<a-form-item label="所属学期">
						<a-select :allowClear="true" v-decorator="['SemesterID',{ rules: [{ required: true, message: '请选择学期' }] }]" placeholder="全部"
							optionFilterProp="children" showSearch>
							<a-select-option v-for="(item, index) in SemesterList" :key="index" :value="item.ID">
								{{ item.Name }}
							</a-select-option>
						</a-select>
					</a-form-item>
					<a-form-item label="团队申报时间">
						<a-range-picker show-time format="YYYY-MM-DD HH:mm:ss" v-decorator="['Data', { rules: [{ required: true, message: '请选择时间' }] }]" />
					</a-form-item>
					<a-form-item label="结项时间">
						<a-range-picker show-time format="YYYY-MM-DD HH:mm:ss" v-decorator="['CloseTime', { rules: [{ required: true, message: '请选择时间' }] }]" />
					</a-form-item>
					<a-form-item label="发放学分时间">
						<a-range-picker show-time format="YYYY-MM-DD HH:mm:ss" v-decorator="['SendScoreTime', { rules: [{ required: true, message: '请选择时间' }] }]" />
					</a-form-item>
					<a-form-item class="fixed-bottom">
						<a-button type="primary" @click="handleSubmit">{{ !text.ID ? '确认添加' : '确认修改' }}</a-button>
						<a-button style="margin-left: 10px;" @click="closeFunction">取消</a-button>
					</a-form-item>
				</a-form>
			</div> 
		</div>
	</div>
</template>

<script>
import moment from 'moment';
import { ManagePracticeSessionDoAdd,DoGetSemesterListCommon, ManagePracticeSessionDoUpdate,CommonDoGetPracticeSessionTypeList } from '@/api/follow';
export default {
	props: {
		text: Object
	},
	watch: {
		text: function(text) {
			this.getOtherList()
			if (!text.ID) {
				this.currentID = ''
			} else {
				this.currentID = text.ID;
				setTimeout(()=> {
					this.form.setFieldsValue({
						Name: text.Name,
						Type:text.Type,
						Score:text.Score,
						SemesterID:text.SemesterID,
						Data: [moment(text.StartTime), moment(text.EndTime)],
						CloseTime: [moment(text.CloseStartTime), moment(text.CloseEndTime)],
						SendScoreTime: [moment(text.SendScoreStartTime), moment(text.SendScoreEndTime)]
					});
				}, 100);
			}
		}
	},
	data() {
		return {
			loading: false,
			formLayout: 'horizontal',
			form: this.$form.createForm(this, {
				name: 'coordinated'
			}),
			currentID: '',
			SemesterList:[],
			TypeList:[]
		};
	},
	created() {},
	methods: {
		moment,
		async getOtherList(){
			let res = await CommonDoGetPracticeSessionTypeList()
			this.TypeList = res.data.data
			let res3 = await DoGetSemesterListCommon();
			this.SemesterList = res3.data.data;
		},
		closeFunction(data) {
			this.form.resetFields();
			this.$emit('closeFun', data);
		},
		handleSubmit(e) {
			this.form.validateFields(async (err, values) => {
				if (!err) {
					let data = {};
					data = values
					data.StartTime = moment(values.Data[0]).format('YYYY-MM-DD HH:mm');
					data.EndTime = moment(values.Data[1]).format('YYYY-MM-DD HH:mm');
					data.CloseStartTime = moment(values.CloseTime[0]).format('YYYY-MM-DD HH:mm');
					data.CloseEndTime = moment(values.CloseTime[1]).format('YYYY-MM-DD HH:mm');
					data.SendScoreStartTime = moment(values.SendScoreTime[0]).format('YYYY-MM-DD HH:mm');
					data.SendScoreEndTime = moment(values.SendScoreTime[1]).format('YYYY-MM-DD HH:mm');
					let res = {};
					if (!this.currentID) {
						res = await ManagePracticeSessionDoAdd(data);
					} else {
						data.id = this.currentID;
						res = await ManagePracticeSessionDoUpdate(data);
					}
					if (res.data.code === 0) {
						this.$message.success(res.data.msg);
						this.closeFunction('1');
					} else {
						this.$message.error(res.data.msg);
					}
				}
			});
		}
	}
};
</script>

<style lang="less" scoped>
</style>
